package com.canomo.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * 自定义的Json转换器
 */
@Component("customObjectMapper")
public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        super();
        SimpleModule simpleModule = new SimpleModule();
        //把Long类型转换成String,防止精度丢失
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        //设置返回的日期格式
        setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        registerModule(simpleModule);
    }
}
